﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using GBFinance.Entity;
using HiLand.Utility.DataBase;
using HiLand.Utility.Paging;
using System.Data;
using HiLand.Utility.Finance;

namespace GBFinance.DAL
{
    public class IncomeAssetDAL
    {
        public virtual bool Create(IncomeAssetEntity entity)
        {
            string commandText = @"Insert Into [LoanIncomeAsset] (
			        [ItemGuid],
			        [ItemKind],
			        [ItemType],
			        [ItemNo],
			        [ItemName],
			        [ItemValue],
			        [UserGuid],
			        [PropertyNames],
			        [PropertyValues]
                ) 
                Values (
			        @ItemGuid,
			        @ItemKind,
			        @ItemType,
			        @ItemNo,
			        @ItemName,
			        @ItemValue,
			        @UserGuid,
			        @PropertyNames,
			        @PropertyValues
                )";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }

        public virtual bool Update(IncomeAssetEntity entity)
        {
            string commandText = @"Update [LoanIncomeAsset] Set   
					[ItemGuid] = @ItemGuid,
					[ItemKind] = @ItemKind,
					[ItemType] = @ItemType,
					[ItemNo] = @ItemNo,
					[ItemName] = @ItemName,
					[ItemValue] = @ItemValue,
					[UserGuid] = @UserGuid,
					[PropertyNames] = @PropertyNames,
					[PropertyValues] = @PropertyValues
             Where [ItemGuid] = @ItemGuid";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }

        public virtual bool Delete(Guid itemGuid)
        {
            string commandText = @"Delete From [LoanIncomeAsset] Where [ItemGuid] = @ItemGuid";

            SqlParameter[] sqlParas = new SqlParameter[] { new SqlParameter("@ItemGuid", itemGuid) };

            bool isSuccessful = SqlHelperEx.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }

        public virtual bool Delete(IncomeAssetEntity entity)
        {
            return Delete(entity.ItemGuid);
        }

        public virtual IncomeAssetEntity Get(Guid itemGuid)
        {
            string commandText = "SELECT * FROM [LoanIncomeAsset] WHERE  [ItemGuid] = @ItemGuid";
            SqlParameter[] sqlParas = new SqlParameter[] { new SqlParameter("@ItemGuid", itemGuid) };
            return SqlGeneral.GetEntity<IncomeAssetEntity>(commandText, sqlParas, Load);
        }

        /// <summary>
        /// 获取新闻类别列表
        /// </summary>
        /// <param name="onlyDisplayUsable"></param>
        /// <param name="whereClause"></param>
        /// <returns></returns>
        public virtual List<IncomeAssetEntity> GetList(string whereClause)
        {
            List<IncomeAssetEntity> collection = new List<IncomeAssetEntity>();
            List<SqlParameter> sqlparaList = new List<SqlParameter>();
            if (string.IsNullOrEmpty(whereClause))
            {
                whereClause = " 1=1 ";
            }

            //if (onlyDisplayUsable == Logics.True)
            //{
            //    whereClause += " AND [CanUsable]= @CanUsable ";
            //    sqlparaList.Add(new SqlParameter("@CanUsable", (int)onlyDisplayUsable));
            //}

            string commandText = "SELECT * FROM [LoanIncomeAsset] WHERE  " + whereClause;
            SqlParameter[] sqlParas = sqlparaList.ToArray();
            collection = SqlGeneral.GetEntityList<IncomeAssetEntity>(commandText, sqlParas, Load);

            return collection;
        }

        public virtual PagedEntityCollection<IncomeAssetEntity> GetPagedCollection(int startIndex, int pageSize, string whereClause, string orderClause)
        {
            return SqlGeneral.GetPagedCollection<IncomeAssetEntity>("usp_Loan_IncomeAsset_SelectPaging", whereClause, orderClause, startIndex, pageSize, Load);
        }


        /// <summary>
        /// 获取总的条目
        /// </summary>
        /// <param name="whereClause"></param>
        /// <returns></returns>
        public virtual int GetTotalCount(string whereClause)
        {
            return SqlGeneral.GetTotalCount("LoanIncomeAsset", whereClause);
        }

        #region 辅助方法
        private static SqlParameter[] PrepareParasAll(IncomeAssetEntity entity)
        {
            List<SqlParameter> list = new List<SqlParameter>() 
            {
			    new SqlParameter("@ItemGuid",entity.ItemGuid),
			    new SqlParameter("@ItemKind",entity.ItemKind),
			    new SqlParameter("@ItemType",entity.ItemType),
			    new SqlParameter("@ItemNo",entity.ItemNo),
			    new SqlParameter("@ItemName",entity.ItemName??string.Empty),
			    new SqlParameter("@ItemValue",entity.ItemValue),
			    new SqlParameter("@UserGuid",entity.UserGuid),
			    new SqlParameter("@PropertyNames",entity.PropertyNames??string.Empty),
			    new SqlParameter("@PropertyValues",entity.PropertyValues??string.Empty)
            };

            return list.ToArray();
        }

        public static IncomeAssetEntity Load(IDataReader reader)
        {
            IncomeAssetEntity entity = new IncomeAssetEntity();
            if (reader != null && reader.IsClosed == false)
            {
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemID"))
                {
                    entity.ItemID = reader.GetInt32(reader.GetOrdinal("ItemID"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemGuid"))
                {
                    entity.ItemGuid = reader.GetGuid(reader.GetOrdinal("ItemGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemKind"))
                {
                    entity.ItemKind = reader.GetInt32(reader.GetOrdinal("ItemKind"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemType"))
                {
                    entity.ItemType = reader.GetInt32(reader.GetOrdinal("ItemType"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemNo"))
                {
                    entity.ItemNo = reader.GetInt32(reader.GetOrdinal("ItemNo"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemName"))
                {
                    entity.ItemName = reader.GetString(reader.GetOrdinal("ItemName"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemValue"))
                {
                    entity.ItemValue = reader.GetDecimal(reader.GetOrdinal("ItemValue"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "UserGuid"))
                {
                    entity.UserGuid = reader.GetGuid(reader.GetOrdinal("UserGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyNames"))
                {
                    entity.PropertyNames = reader.GetString(reader.GetOrdinal("PropertyNames"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyValues"))
                {
                    entity.PropertyValues = reader.GetString(reader.GetOrdinal("PropertyValues"));
                }
            }
            return entity;
        }
        #endregion
    }
}
